package com.huilian.iotbox.admin.controller;

import com.huilian.iotbox.admin.service.CommodityStockService;
import com.huilian.iotbox.admin.service.UserService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.dto.CommodityStockRecordDto;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @author LinYinDong
 * @date 2024/9/14 下午3:00
 */
@RequestMapping("/api/commodityStock")
@RestController
public class CommodityStockController extends BaseController {

    @Autowired
    private UserService userService;
    @Resource
    private CommodityStockService commodityStockService;

    @PostMapping("/findCommodityStockList")
    public ServerResponse findCommodityStockList(HttpServletRequest request , @RequestBody CommodityStockRecordDto commodityStockRecordDto)  {
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        UserInfo userInfo = userService.getLoginUserInfo(request);
        //不等于1，3，5，无权限操作
        Integer[] roleIdArray = {
                RoleEnum.MERCHANT.getRoleId(),
                RoleEnum.SUPER_ADMINISTRATOR.getRoleId(),
                RoleEnum.SUB_ACCOUNT.getRoleId()
        };
        boolean contains = Arrays.asList(roleIdArray).contains(userInfo.getRoleId());
        if(!contains){
            return ServerResponse.createByErrorMessage("暂无操作权限！");
        }
        commodityStockRecordDto.setUserInfo(userInfo);
        commodityStockRecordDto.setPageNum(Integer.parseInt(pageNum));
        commodityStockRecordDto.setPageSize(Integer.parseInt(pageSize));
        return ServerResponse.createBySuccess(commodityStockService.findCommodityStockList(commodityStockRecordDto));
    }


    @PostMapping("/exportCommodityStockList")
    public ServerResponse exportCommodityStockList(HttpServletRequest request , @RequestBody CommodityStockRecordDto commodityStockRecordDto)  {
        UserInfo userInfo = userService.getLoginUserInfo(request);
        //不等于1，3，5，无权限操作
        Integer[] roleIdArray = {
                RoleEnum.MERCHANT.getRoleId(),
                RoleEnum.SUPER_ADMINISTRATOR.getRoleId(),
                RoleEnum.SUB_ACCOUNT.getRoleId()
        };
        boolean contains = Arrays.asList(roleIdArray).contains(userInfo.getRoleId());
        if(!contains){
            return ServerResponse.createByErrorMessage("暂无操作权限！");
        }

        if (RoleEnum.SUPER_ADMINISTRATOR.getRoleId().equals(userInfo.getRoleId()) && StringUtils.isEmpty(commodityStockRecordDto.getUserId())) {
            return ServerResponse.createByErrorMessage("请按单个账号导出");
        }
        commodityStockRecordDto.setUserInfo(userInfo);
        return commodityStockService.exportCommodityStockList(commodityStockRecordDto);
    }
}
